package com.webinsky.service.impl.user;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.webinsky.domain.user.Account;
import com.webinsky.domain.user.Role;
import com.webinsky.persist.dao.user.UserMapper;
import com.webinsky.persist.entity.user.User;
import com.webinsky.service.user.AuthorizationManagement;

@Service
public class AuthorizationManagementImpl implements AuthorizationManagement {
	
	private static final Logger logger = LoggerFactory.getLogger(AuthorizationManagementImpl.class);
	
	@Autowired
	private UserMapper userDao;
	
	private Account convert(User user){
		Account account = new Account();
		account.setId(user.getId());
		account.setLoginName(user.getLogin_name());
		account.setUserName(user.getName());
		account.setPassword(user.getLogin_password());
		account.setUid(String.valueOf(user.getId()));
		account.setSiteId(user.getSiteId());
		Role role = Role.parse(user.getRole());
		if ( role != null ){
			account.setRole(role.getRoleId());
		}
		return account;
	}
	
	private User convert(Account account){
		User user = new User();
		user.setLogin_name(account.getLoginName());
		user.setLogin_password(account.getPassword());
		user.setName(account.getUserName());
		user.setRole(account.getRole());
		user.setSiteId(account.getSiteId());
		user.setDel(0);
		return user;
	}	
	
	@Override
	public void register(Account account) {
		if ( account != null && 
				!StringUtils.isEmpty(account.getUserName()) && 
					!StringUtils.isEmpty(account.getPassword()) && 
						!StringUtils.isEmpty(account.getLoginName()) ){
			// 检查这个用户是否已经存在
			List<User> users = userDao.selectByLoginName(account.getLoginName());
			if ( users.isEmpty() ){
				User user = convert(account);
				user.setDel(0);
				userDao.insert(user);
				return;
			}
			logger.error("[register] Account = " + account + " 已经存在 ");
			throw new RuntimeException("用户已经存在");
		}
		logger.error("[register] 参数不对");
		throw new RuntimeException("参数User = null");
	}


	@Override
	public Account login(String userName, String password) {
		if ( !StringUtils.isEmpty(userName) && !StringUtils.isEmpty(password) ){
			List<User> users = userDao.selectByDelLoginNameAndLoginPassword(userName, password, 0);
			if ( users != null && users.size() == 1 ){
				return convert(users.get(0));
			}
		}
		logger.error("[login] 用户名或则密码为空");
		return null;
	}

	@Override
	public void restPassword(int userId, String oldPassword, String newPassword) {
		if ( userId > 0 && !StringUtils.isEmpty(oldPassword) && !StringUtils.isEmpty(newPassword) ){
			if ( !oldPassword.equals(newPassword) ) {
				User user = userDao.selectByIdAndDel(userId, 0);
				if ( user != null && oldPassword.equals(user.getLogin_password()) ){
					user.setLogin_password(newPassword);
					userDao.updateByPrimaryKey(user);
				}
			}
		}
		throw new RuntimeException("参数不正确");
	}

	@Override
	public void updateStatus(Integer uid, int actived) {
		if ( uid != null && actived == 0 || actived == 1){
			User user = userDao.selectById(uid);
			if ( user != null ){
				user.setDel(actived);
				userDao.updateByPrimaryKey(user);
			}
		}
	}

	@Override
	public void delUser(Integer uid) {
		if ( uid != null ){
			User user = userDao.selectById(uid);
			if ( user != null ){
				user.setDel(1);
				userDao.updateByPrimaryKey(user);
			}
		}
	}

	@Override
	public Account getByUserId(int userId) {
		if ( userId > 0 ){
			User user = userDao.selectById(userId);
			if ( user != null ){
				return convert(user);
			}
		}
		return null;
	}

}
